package com.cooper.controllers.seller;

import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.constants.ResourceConstants;
import com.cooper.controllers.Controller;
import com.cooper.controllers.LoginRequired;
import com.cooper.entity.LikeDetailEntity;
import com.cooper.entity.UserInfo;
import com.cooper.service.item.LikeDetailService;
import com.cooper.service.seller.ShopStatService;
import com.cooper.util.Controllers;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;

/**
 * 卖家店铺的统计信息
* @Description: 
* @version: v1.0.0
* @author: tianlg
* @date: Mar 13, 2015 11:43:02 PM
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* Mar 13, 2015      tianlg          v1.0.0
 */
@LoginRequired
public class StatisticsController extends Controller {
	
	@Autowired
	private ShopStatService shopStatService;
	
	@Autowired
	private LikeDetailService likeDetailService;

	@Get("like") @Post("like")
	public Object like(Invocation inv, @Param("sellerId") Long sellerId) throws Exception{
		UserInfo userInfo = Controllers.getUserInfo(inv);
		
		if(sellerId!=null&&userInfo!=null){
			LikeDetailEntity detailEntity = this.likeDetailService.getEntityByUserId(ResourceConstants.LikeOrigin.SHOP_LIKE, sellerId, userInfo.getId());
		    if(detailEntity!=null){
		    	if(detailEntity.getScore()==1){
		    		return this.shopStatService.unlike(detailEntity, userInfo,ResourceConstants.LikeOrigin.SHOP_LIKE);
		    	}else if(detailEntity.getScore()==-1){
		    		return this.shopStatService.like(detailEntity, userInfo,ResourceConstants.LikeOrigin.SHOP_LIKE);
		    	}
		    }else{
		    	LikeDetailEntity likeDetail = new LikeDetailEntity();
				likeDetail.setOuterId(sellerId);
				likeDetail.setUserId(userInfo.getId());
				likeDetail.setClientIp(Controllers.getClientIp(inv));
				return this.shopStatService.like(likeDetail, userInfo,ResourceConstants.LikeOrigin.SHOP_LIKE);
		    }
		}
		
		return null;
	}
	
}
